package com.hhit.bigscreenbackmanage.service.impl.zlgl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhit.bigscreenbackmanage.dao.zlgl.TZlglSbdlsdtyzsfgcsbxmMapper;
import com.hhit.bigscreenbackmanage.entity.zlgl.TZlglActualMeasurement;
import com.hhit.bigscreenbackmanage.entity.zlgl.TZlglSbdlsdtyzsfgcsbxm;
import com.hhit.bigscreenbackmanage.service.impl.jsgl.TJsqlTysjBdzServiceImpl;
import com.hhit.bigscreenbackmanage.service.zlgl.TZlglSbdlsdtyzsfgcsbxmService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class TZlglSbdlsdtyzsfgcsbxmServiceImpl implements TZlglSbdlsdtyzsfgcsbxmService {

    private Logger log = LoggerFactory.getLogger(TJsqlTysjBdzServiceImpl.class);

    @Autowired
    private TZlglSbdlsdtyzsfgcsbxmMapper tZlglSbdlsdtyzsfgcsbxmMapper;

    @Override
    public PageInfo<TZlglSbdlsdtyzsfgcsbxm> getTZlglSbdlsdtyzsfgcsbxmAll(JSONObject json) throws Exception{
        if (json.get("dataTime") == null || "".equals(json.get("dataTime"))){
            new Exception("需要数据的日期为空！");
        }
        int pageNum = !json.containsKey("pageNum") ? 1 : Integer.parseInt(json.get("pageNum").toString());
        int pageSize = !json.containsKey("pageSize") ? 15 : Integer.parseInt(json.get("pageSize").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<TZlglSbdlsdtyzsfgcsbxm> list = tZlglSbdlsdtyzsfgcsbxmMapper.getTZlglSbdlsdtyzsfgcsbxmAll(json);

        PageInfo<TZlglSbdlsdtyzsfgcsbxm> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertTZlglSbdlsdtyzsfgcsbxm(MultipartFile[] file, String dataTime, String propName) throws Exception {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TZlglSbdlsdtyzsfgcsbxm> list = (List<TZlglSbdlsdtyzsfgcsbxm>) Utils.mapToEntiry(result, TZlglSbdlsdtyzsfgcsbxm.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TZlglSbdlsdtyzsfgcsbxm tZlglSbdlsdtyzsfgcsbxm : list) {
                        tZlglSbdlsdtyzsfgcsbxm.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tZlglSbdlsdtyzsfgcsbxm.setDataTime(dataTime);
                    }
                    // 查询dataTime符合的数据，进行删除
                    List<TZlglSbdlsdtyzsfgcsbxm> tZlglSbdlsdtyzsfgcsbxmList = tZlglSbdlsdtyzsfgcsbxmMapper.getTZlglSbdlsdtyzsfgcsbxmByDataTime(dataTime);
                    if (CollectionUtil.isNotEmpty(tZlglSbdlsdtyzsfgcsbxmList)){
                        List<String> ids = tZlglSbdlsdtyzsfgcsbxmList.stream().map(TZlglSbdlsdtyzsfgcsbxm::getId).collect(Collectors.toList());
                        int deleteRes = tZlglSbdlsdtyzsfgcsbxmMapper.batchDeleteZlglSbdlsdtyzsfgcsbxmByIds(ids);
                        log.info("insertTZlglSbdlsdtyzsfgcsbxm==>deleteRes:{}",deleteRes);
                    }
                    tZlglSbdlsdtyzsfgcsbxmMapper.insertTZlglSbdlsdtyzsfgcsbxm(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public int getPilotProjectStatistics(JSONObject json) {
        return tZlglSbdlsdtyzsfgcsbxmMapper.getPilotProjectStatistics(json);
    }

    @Override
    public JSONObject getLsdtyzgcTj(JSONObject json) {
        return tZlglSbdlsdtyzsfgcsbxmMapper.getLsdtyzgcTj(json);
    }

}
